package com.example.zuul;

import com.google.common.util.concurrent.RateLimiter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.exception.ZuulException;

public class LimitFilter extends ZuulFilter {

    static final RateLimiter rateLimiter = com.google.common.util.concurrent.RateLimiter.create(20);

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        // header token


        return true;
    }

    @Override
    public Object run() throws ZuulException {

        if(!rateLimiter.tryAcquire()) {
            throw new RuntimeException("已被限流！！！");
        }

        System.out.println("正常，没有被限流！");

        return null;
    }
}
